home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 8 / Engine / Material.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  4.5 KB  |  120 lines

  1. //-----------------------------------------------------------------------------
  2. // Material.h implementation.
  3. // Refer to the Material.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // The material class constructor.
  12. //-----------------------------------------------------------------------------
  13. Material::Material( char *name, char *path ) : Resource< Material >( name, path )
  14. {
  15.     D3DXIMAGE_INFO info;
  16.  
  17.     // Load the script for this material.
  18.     Script *script = new Script( name, path );
  19.  
  20.     // Check if the material's texture has transparency.
  21.     if( script->GetColourData( "transparency" )->a == 0.0f )
  22.     {
  23.         // Load the texture without transparency.
  24.         D3DXCreateTextureFromFileEx( g_engine->GetDevice(), script->GetStringData( "texture" ), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE, D3DX_FILTER_TRIANGLE, 0, &info, NULL, &m_texture );
  25.     }
  26.     else
  27.     {
  28.         // Load the texture using a transparency colour value.
  29.         D3DCOLORVALUE *colour = script->GetColourData( "transparency" );
  30.         D3DCOLOR transparency = D3DCOLOR_COLORVALUE( colour->r, colour->g, colour->b, colour->a );
  31.         D3DXCreateTextureFromFileEx( g_engine->GetDevice(), script->GetStringData( "texture" ), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE, D3DX_FILTER_TRIANGLE, transparency, &info, NULL, &m_texture );
  32.     }
  33.  
  34.     // Store the width and height of the texture.
  35.     m_width = info.Width;
  36.     m_height = info.Height;
  37.  
  38.     // Set the material's lighting properties.
  39.     m_lighting.Diffuse = *script->GetColourData( "diffuse" );
  40.     m_lighting.Ambient = *script->GetColourData( "ambient" );
  41.     m_lighting.Specular = *script->GetColourData( "specular" );
  42.     m_lighting.Emissive = *script->GetColourData( "emissive" );
  43.     m_lighting.Power = *script->GetFloatData( "power" );
  44.  
  45.     // Set the ignore face flag.
  46.     m_ignoreFace = *script->GetBoolData( "ignore_face" );
  47.  
  48.     // Set the ignore fog flag.
  49.     m_ignoreFog = *script->GetBoolData( "ignore_fog" );
  50.  
  51.     // Set the ignore ray flag.
  52.     m_ignoreRay = *script->GetBoolData( "ignore_ray" );
  53.  
  54.     // Destory the material's script.
  55.     SAFE_DELETE( script );
  56. }
  57.  
  58. //-----------------------------------------------------------------------------
  59. // The material class destructor.
  60. //-----------------------------------------------------------------------------
  61. Material::~Material()
  62. {
  63.     SAFE_RELEASE( m_texture );
  64. }
  65.  
  66. //-----------------------------------------------------------------------------
  67. // Returns the material's texture.
  68. //-----------------------------------------------------------------------------
  69. IDirect3DTexture9 *Material::GetTexture()
  70. {
  71.     return m_texture;
  72. }
  73.  
  74. //-----------------------------------------------------------------------------
  75. // Returns the material's lighting properties.
  76. //-----------------------------------------------------------------------------
  77. D3DMATERIAL9 *Material::GetLighting()
  78. {
  79.     return &m_lighting;
  80. }
  81.  
  82. //-----------------------------------------------------------------------------
  83. // Returns the width of the material's texture.
  84. //-----------------------------------------------------------------------------
  85. unsigned long Material::GetWidth()
  86. {
  87.     return m_width;
  88. }
  89.  
  90. //-----------------------------------------------------------------------------
  91. // Returns the height of the material's texture.
  92. //-----------------------------------------------------------------------------
  93. unsigned long Material::GetHeight()
  94. {
  95.     return m_height;
  96. }
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Returns the material's ignore face flag.
  100. //-----------------------------------------------------------------------------
  101. bool Material::GetIgnoreFace()
  102. {
  103.     return m_ignoreFace;
  104. }
  105.  
  106. //-----------------------------------------------------------------------------
  107. // Returns the material's ignore fog flag.
  108. //-----------------------------------------------------------------------------
  109. bool Material::GetIgnoreFog()
  110. {
  111.     return m_ignoreFog;
  112. }
  113.  
  114. //-----------------------------------------------------------------------------
  115. // Returns the material's ignore ray flag.
  116. //-----------------------------------------------------------------------------
  117. bool Material::GetIgnoreRay()
  118. {
  119.     return m_ignoreRay;
  120. }